﻿/// <reference path="../../../amplify.js" />
/// <reference path="../../../amplify-vsdoc.js" />
/// <reference path="C:\ProgramasMary\ExcellentProject3\v3.0\Dev\Sources\ExcellentProjectPlus v2\ExcellentProjectPlus\lib/arrayfinder.js" />
/// <reference path="../../../helpers/validationHelper.js" />
YUI.add('editar-recurso-view', function (Y) {
    'use strict';
    Y.EditarRecurso = Y.Base.create('EditarRecurso', Y.View, [], {
        templateIsCharged: false,
        loadTemplate: function () {
            if (this.template == '') {
                var that = this,
                validator = null,
                request = Y.io('/ExcellentProject/scripts/views/cuestionarios/recursos/templates/editar.html', {
                    on: {
                        complete: function (id, response) {
                            that.templateIsCharged = true;
                            that.template = response.responseText;
                            that.render();
                        }
                    },
                    failure: function (id, response) {
                        bootbox.alert(response)
                    }
                })
            }
        },
        events: {
            '.guardar': {
                click: 'guardarRecurso'
            }
        },
        render: function () {
            this.loadTemplate();
            if (this.templateIsCharged) {
                var contentTemplate = Y.Handlebars.compile(this.template);
                var catalogosMultimedia = amplify.store('catalogos')
                var listaCatalogos = catalogosMultimedia.find(function (a) { return a.Clave == "Multimedia"; })
                this.get('container').setHTML(contentTemplate(Y.merge(this.get('model').getAttrs(), { CatalogosMultimedia: listaCatalogos  })));
                $('#nombre').alpha();
            }
        },
        guardarRecurso: function (ev) {
            var model = this.get('model')
            var requiredMessages = '';

            //validacion de campos requeridos
            if (Y.one('#nombre').get('value').trim() == '') {
                Y.one('#nombreGrupo').removeClass('has-success')
                Y.one('#nombreGrupo').toggleClass('has-error', true)
                requiredMessages = ' - Nombre Requerido. <br/>';

            } else {
                Y.one('#nombreGrupo').removeClass('has-error')
                Y.one('#nombreGrupo').toggleClass('has-success', true)
            }
            if (requiredMessages != '') {
                bootbox.dialog({
                    message: requiredMessages,
                    title: "Problemas de Validación",
                    show: true,
                    backdrop: true,
                    closeButton: true,
                    animate: true,

                    className: "my-modal",
                    buttons: {
                        success: {
                            label: "Ok",
                            className: "btn btn-info",

                            callback: function () { }
                        }
                    }
                });
                return;
            }


            model.setAttrs({
                Nombre: Y.one('#nombre').get('value'),
                CodigoTipo: Y.one('#codigotipo').get('value'),
                Uri: document.getElementById('uploadform').contentWindow.document.getElementById('filename').value
            });
            var options = null;
            if ((model.get('IdRecurso') != 0)) {
                options = { action: 'update', idRec: model.get('IdRecurso') };
                if (model.get('Uri') == '')
                    model.set('Uri', Y.one('#urlValue').get('value'))
            }
            else
                options = { idRec: '' }
            var that = this, a = model.save(options, function (err, response) {
                if (err != null)
                    bootbox.alert('Ocurrieron problemas para actualizar el recurso' + err)
                else {
                    
                    bootbox.alert('Recurso Guardado Exitosamente');
                }

                Y.one('#cancelar').simulate("click");
            });
        }

    })
}, '1.0.0.0', { requires: ['handlebars', 'handlebars-select-helper', 'node', 'aui-form-validator', 'uploader', 'event', 'node-event-simulate'] })